﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.AI;

public abstract class ICharacter
{

    protected ICharacterAttr mAttr;
    /// <summary>
    /// 游戏角色游戏物体
    /// </summary>
    protected GameObject mGameObject;
    /// <summary>
    /// 导航组件
    /// </summary>
    protected NavMeshAgent mNavAgent;
    /// <summary>
    /// 播放组件
    /// </summary>
    protected AudioSource mAudio;

    /// <summary>
    /// mGameObject获取位置
    /// </summary>
    public Vector3 Position {
        get {
            if (mGameObject == null)
            {
                Debug.LogError("mGameObject为空");
                return Vector3.zero;
            }
            return mGameObject.transform.position;
        }
    }

    /// <summary>
    /// 动画组件
    /// </summary>
    protected Animation mAnim;
    /// <summary>
    /// 武器
    /// </summary>
    protected IWeapon mWeapon;

    /// <summary>
    /// 在角色里统一进行组件的更新调用
    /// </summary>
    public void Update()
    {
        //武器更新
        mWeapon.Update();
    }
    public IWeapon weapon {
        set { mWeapon = value; }
    }
    /// <summary>
    /// 获取攻击距离
    /// </summary>
    public float atkRange
    {
        get { return mWeapon.atkRange; }
    }

    public abstract void UpdateFSMAI(List<ICharacter> targets);
    public void Attack(ICharacter target )
    {
        if (mWeapon != null) 
        { 
            mWeapon.Fire(target.Position);
        }
        else
        {
            Debug.LogError("请设置武器给角色");
        }
       //朝向
        mGameObject.transform.LookAt(target.Position);
        PlayAnim("attack");
        target.UnderAttack(mWeapon.atk+mAttr.critValue);
    }
    /// <summary>
    /// 处理掉血
    /// </summary>
    public virtual void UnderAttack(int damage)
    {
        mAttr.TakeDamage(damage);
         

    }
    /// <summary>
    /// 游戏物体死亡
    /// </summary>
    public void Killed()
    {
        //todo 死亡处理
    }
    /// <summary>
    /// 播放特效
    /// </summary>
    /// <param name="effectName"></param>
    protected void DoPlayEffect(string effectName)
    {
        //todo 第一步 加载特效effect 用统一资源加载类
        GameObject effectGO;
        //todo 控制销毁  

    }
    /// <summary>
    /// 播放声音
    /// </summary>
    /// <param name="soundname"></param>
    protected void DoPlauSound(string soundname)
    {
        AudioClip clip = null; //todo  暂时如此  之后需要用资源管理类赋值
        mAudio.clip = clip;
        mAudio.Play();
    }
    /// <summary>
    /// 播放动画
    /// </summary>
    /// <param name="animName"></param>
    public void PlayAnim(string animName)
    {
        mAnim.CrossFade(animName);
    }
    /// <summary>
    /// 移动到敌人位置
    /// </summary>
    /// <param name="targetPosition"></param>
    public void MoveTo(Vector3 targetPosition)
    {
        mNavAgent.SetDestination(targetPosition);
        PlayAnim("move");
    }
}
 
